/**
 * @(#) ContractProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 계약현황 조회 - Proxy
 * 				 
 */
package com.skt.common_hr.search.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.search.SearchFacade;
	import com.skt.common_hr.search.model.vo.SearchResultVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class SearchProxy extends CommonProxy
	{
		public static const NAME:String = "SearchProxy";
		
		public var searchResult:ArrayCollection;
		public var msgStr:String = "";


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function SearchProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function search(keyword:String, condition:Array = null):void
		{
			var param:Object = new Object();
			param["I_SEAR_TEXT"] = keyword;
			if(condition!=null) {
				var tables:Object = new Object;
				tables["ITAB"] = condition;
				//trace(condition[0].ORGEH);
				saveData("Z_HR_0070", param, tables, result, fault);
			}
			else {
				loadData("Z_HR_0070", param, result, fault);
			} 
		}
		







		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				trace("detail suss");
				searchResult = parseResult(event.result.OTAB);
				facade.sendNotification(SearchFacade.LOAD_SEARCH_COMPLETE, this);
			}
			else {
				trace( "failed to load 인사검색: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(SearchFacade.LOAD_SEARCH_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in ContractPRoxy: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(SearchFacade.LOAD_SEARCH_FAILED, this);
		}
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 공지사항 Value Object
		 */
		 private function parseResult(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			
			for each(var rec:Object in src)
			{
				var vo:SearchResultVO = new SearchResultVO();
				
				vo.PERNR		= StringUtil.trimFront(rec.PERNR, "0");		//사번
				vo.PHOTO_URL	= rec.PHOTO_URL;	//성명
				if(vo.PHOTO_URL.length<1) {
					vo.PHOTO_URL = "assets/images/noimage.swf";
				}
				vo.ENAME		= rec.ENAME;		//부서명
				vo.ORGTX		= rec.ORGTX;		//내용
				vo.CATEGORY		= rec.CATEGORY;		//게시일자(수정일자)
				vo.CATEGORYT	= rec.CATEGORYT;	//사원 또는 지원자의 포맷된 이름
				vo.BEGDA		= rec.BEGDA;		//조회수
				vo.ENDDA		= rec.ENDDA;		//파일이름
				vo.KEYWORD		= rec.KEYWORD;
				vo.BAND 		= rec.BAND;			//BAND
				vo.PERSK 		= rec.PERSK;		//사원하위그룹
				vo.IDATE 		= DateUtil.stringToDateTime(rec.IDATE);		//입사일
				vo.PLANST 		= rec.PLANST;		//오브젝트이름	( 직책 )
				vo.PTEXT 		= rec.PTEXT;		//사원 하위 그룹 이름 ( 직군 )
				vo.STELLT 		= rec.STELLT;		//오브젝트이름 ( 직무 )
				
				list.addItem(vo);
			}
			return list;
		}
	}
}